<template>
  <el-dialog v-model="show" :title="title" append-to-body :close-on-click-modal="false">
    <el-form ref="hotelRoomRef" :model="form" :rules="rules" label-width="120px" :inline="true">
      <el-row :gutter="20">
        <el-col>
          <el-form-item label="房间图片" prop="photo">
            <image-upload2 v-model="form.photo" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="房型" prop="roomType" style="width: 100%">
            <el-input v-model="form.roomType" placeholder="请输入房型" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="床型" prop="bedType" style="width: 100%">
            <el-input v-model="form.bedType" placeholder="请输入床型" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入住人数" prop="occupantsNum" style="width: 100%">
            <el-input v-model="form.occupantsNum" type="number" placeholder="请输入入住人数" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否有窗户" prop="windowFlag" style="width: 100%">
            <el-select v-model="form.windowFlag" placeholder="请选择" style="width: 100%">
              <el-option label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否含早餐" prop="breakfastFlag" style="width: 100%">
            <el-select v-model="form.breakfastFlag" placeholder="请选择" style="width: 100%">
              <el-option label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>

          </el-form-item>
        </el-col>
        <el-col v-show="form.breakfastFlag=='1'" :span="12">
          <el-form-item label="早餐数" prop="breakfastNum" style="width: 100%">
            <el-input v-model="form.breakfastNum" type="number" placeholder="请输入早餐数" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否有独立卫浴" prop="bathroomFlag" style="width: 100%">
            <el-select v-model="form.bathroomFlag" placeholder="请选择" style="width: 100%">
              <el-option label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否可加床" prop="addBedFlag" style="width: 100%">
            <el-select v-model="form.addBedFlag" placeholder="请选择" style="width: 100%">
              <el-option label="是" value="1" />
              <el-option label="否" value="0" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="面积" prop="area" style="width: 100%">
            <el-input v-model="form.area" type="number" placeholder="请输入面积" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="show=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>

import { getCurrentInstance, ref, toRefs, watch } from 'vue'
import useVenStore from '@/store/modules/ven'
import { reactive } from '@vue/runtime-core'
import { addHotelRoom, updateHotelRoom } from '@/api/logex/hotelRoom'

const { proxy } = getCurrentInstance()
const emit = defineEmits(['submit'])

const show = ref(false)
const title = ref('')
const buttonLoading = ref(false)

const data = reactive({
  form: {
    id: null,
    venueId: null,
    hotelId: null,
    photo: null,
    roomType: null,
    bedType: null,
    occupantsNum: null,
    breakfastFlag: null,
    breakfastNum: null,
    windowFlag: null,
    bathroomFlag: null,
    addBedFlag: null,
    area: null,
    status: '1',
    delStatus: '0'
  },
  rules: {
    photo: [
      { required: true, message: '图片不能为空', trigger: 'blur' }
    ],
    roomType: [
      { required: true, message: '房型不能为空', trigger: 'blur' }
    ],
    bedType: [
      { required: true, message: '床型不能为空', trigger: 'blur' }
    ],
    occupantsNum: [
      { required: true, message: '入住人数不能为空', trigger: 'blur' }
    ],
    breakfastFlag: [
      { required: true, message: '是否含早餐不能为空', trigger: 'blur' }
    ],
    breakfastNum: [
      {
        validator: (rule, value, callback) => {
          if (form.value.breakfastFlag == '1') {
            if (!form.value.breakfastNum || parseInt(form.value.breakfastNum) != form.value.breakfastNum || form.value.breakfastNum < 1) {
              callback('请输入正确的早餐数')
            } else {
              callback()
            }
          } else {
            callback()
          }
        }, required: true, trigger: ['blur']
      }
    ],
    windowFlag: [
      { required: true, message: '是否有窗户不能为空', trigger: 'change' }
    ],
    bathroomFlag: [
      { required: true, message: '是否有独立卫浴不能为空', trigger: 'change' }
    ],
    addBedFlag: [
      { required: true, message: '是否可以加床不能为空', trigger: 'change' }
    ],
    area: [
      { required: true, message: '房间面积不能为空', trigger: 'blur' }
    ]
  }
})
const { form, rules } = toRefs(data)

function submitForm() {
  proxy.$refs['hotelRoomRef'].validate(valid => {
    if (valid) {
      buttonLoading.value = true
      if (form.value.id != null) {
        updateHotelRoom(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          show.value = false
          emit('submit', form.value.hotelId)
        }).finally(() => {
          buttonLoading.value = false
        })
      } else {
        addHotelRoom(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          show.value = false
          emit('submit', form.value.hotelId)
        }).finally(() => {
          buttonLoading.value = false
        })
      }
    }
  })
}

watch(show, (val) => {
  if (!val) {
    reset()
  }
})

function reset() {
  form.value = {
    id: null,
    venueId: null,
    hotelId: null,
    photo: null,
    roomType: null,
    bedType: null,
    occupantsNum: null,
    breakfastFlag: null,
    breakfastNum: null,
    windowFlag: null,
    bathroomFlag: null,
    addBedFlag: null,
    area: null,
    status: '1',
    delStatus: '0'
  }
  proxy.resetForm('hotelRoomRef')
}

function open(params) {
  reset()

  show.value = true
  title.value = params.title
  if (params.room) {
    form.value = params.room
  } else {
    form.value.venueId = useVenStore().venueInfo.id
    form.value.hotelId = params.hotelId
  }
}
defineExpose({
  open
})
</script>

<style scoped>
.mx-1{margin: 5px}
</style>
